package com.yc.d0316.biz;

import com.yc.util.DBHelper;

import java.sql.Date;

public class BookBiz {

    public void create(String bookname,String bookauthor,
                       String bookpress,String pressdate,String bookimage)
        throws BizException{
        check(bookname,bookauthor,bookpress,pressdate,bookimage);

        String sql ="insert into bm_book(bookname,bookauthor,bookpress,pressdate,bookimage)" +
                " values (?,?,?,?,?)";

        DBHelper.update(sql,bookname,bookauthor,bookpress,
                Date.valueOf(pressdate), bookimage);
    }

    public void check(String bookname,String bookauthor,
                      String bookpress,String pressdate,String bookimage)throws BizException{
        if (bookname==null || bookname.trim().isEmpty()){
            throw new BizException("请输入书名");
        }
        if (bookpress.length()<3){
            throw  new BizException("出版社至少是三个字符");
        }
        //mysql的默认日期格式  yyyy-mm-dd
        if (pressdate.matches("\\d{4}-\\d{1,2}-\\d{1,2}") == false){
            throw new BizException("出版日期格式不对");
        }
    }




    public void modify(String id, String bookname,String bookauthor,
                       String bookpress,String pressdate,String bookimage)
            throws BizException{

        check(bookname,bookauthor,bookpress,pressdate,bookimage);
        String sql ="update bm_book set bookname=?,bookauthor=?,bookpress=?,pressdate=?,bookimage=?" +
                " where id=?";

        DBHelper.update(sql,bookname,bookauthor,bookpress,
                Date.valueOf(pressdate), bookimage,id);
    }

    //物理删除
    public void remove(String id){
        //删除前要判断，该数据是否被其他表引用
        String sql = "delete from bm_book where id=?";
        DBHelper.update(sql,id);
    }

    //逻辑删除  新增状态字段：status=1正常，status=0删除
    //修改查询语句 条件 + where status = 1
    public void remove1(String id){
        //删除前要判断，该数据是否被其他表引用
        String sql = "update bm_book set status = 0 where id=?";
        DBHelper.update(sql,id);
    }

}
